package interview.huawei.tjt.training.str;

import java.util.Scanner;

/**
 * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 *
 * 示例 1： 输入：s = "We are happy."
 * 输出："We%20are%20happy."
 */
// 双指针法
public class ReplaceSpace {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            char[] ch = sc.nextLine().toCharArray();
            int count = 0;
            for (int i = 0; i < ch.length; i ++) {
                if (ch[i] == ' ') {
                    count ++;
                }
            }
            if (count == 0) {
                System.out.println(new String(ch));
            } else {
                char[] result = new char[ch.length + 2 * count];
                int j = result.length - 1;
                for (int i = ch.length - 1; i >= 0; i --) {
                    if (ch[i] != ' ') {
                        result[j] = ch[i];
                    } else {
                        result[j] = '0';
                        result[j - 1] = '2';
                        result[j - 2] = '%';
                        j -= 2;
                    }
                    j --;
                }
                System.out.println(new String(result));
            }
        }
    }

}
